home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / InputEngine.cpp < prev    next >
C/C++ Source or Header  |  2005-11-18  |  2KB  |  117 lines

  1.  
  2. #include "FileLogger.h"
  3. #include "IniConfigReader.h"
  4. #include "InputEngine.h"
  5.  
  6. namespace peon
  7. {
  8.     template<> InputEngine* ISingleton<InputEngine>::ms_Singleton = 0;
  9.  
  10.     InputEngine* InputEngine::getSingletonPtr(void)
  11.     {
  12.         return ms_Singleton;
  13.     }
  14.  
  15.     InputEngine& InputEngine::getSingleton(void)
  16.     {  
  17.  
  18.         assert( ms_Singleton );  
  19.  
  20.         return ( *ms_Singleton ); 
  21.     }
  22.  
  23.     InputEngine::InputEngine()
  24.     {
  25.         m_bJoystickConnected = false;
  26.         m_pJoystick = NULL;
  27.  
  28.     }
  29.  
  30.     InputEngine::~InputEngine()
  31.     {
  32.         unloadEngine();
  33.     }
  34.  
  35.     bool InputEngine::loadEngine( IniConfigReader* pConfig )
  36.     {
  37.         TCHAR strOutput[MAX_PATH];
  38.  
  39.         //check number of joysticks found
  40.         if(SDL_NumJoysticks() < 1)
  41.         {
  42.             //no connected joysticks found
  43.             FileLogger::getSingleton().logInfo("InputEngine", "No joystick devices connected");
  44.             m_bJoystickConnected = false;
  45.             m_pJoystick = NULL;
  46.             return true;
  47.         }
  48.  
  49.         //just spit out the name of the joystick found..
  50.         sprintf( strOutput, "Joystick detected with name: %s", SDL_JoystickName(0));
  51.         FileLogger::getSingleton().logDebug("InputEngine", strOutput);
  52.  
  53.         m_pJoystick = SDL_JoystickOpen(0);
  54.  
  55.         int iNumberOfButtons = SDL_JoystickNumButtons( m_pJoystick );
  56.         int iNumberOfAxis    = SDL_JoystickNumAxes( m_pJoystick );
  57.  
  58.         //a guard against other input devices disguised as joysticks
  59.         if(iNumberOfAxis < 2)
  60.         {
  61.             SDL_JoystickClose( m_pJoystick );
  62.             m_bJoystickConnected = false;
  63.             m_pJoystick = NULL;
  64.  
  65.             sprintf( strOutput, "Joystick %s doesn't have more than 2 axis. Disabling Joystick support", SDL_JoystickName(0));
  66.             FileLogger::getSingleton().logInfo("InputEngine", strOutput);
  67.  
  68.             return true;
  69.         }
  70.  
  71.  
  72.         sprintf( strOutput, "Joystick properties: %d buttons and %d axis", iNumberOfButtons, iNumberOfAxis);
  73.         FileLogger::getSingleton().logDebug("InputEngine", strOutput);
  74.  
  75.  
  76.         m_bJoystickConnected = true;
  77.  
  78.         return true;
  79.     }
  80.  
  81.     void InputEngine::unloadEngine()
  82.     {
  83.         if(m_pJoystick)
  84.         {
  85.             SDL_JoystickClose( m_pJoystick );
  86.             m_pJoystick = NULL;
  87.         }
  88.  
  89.         m_bJoystickConnected = false;
  90.  
  91.  
  92.     }
  93.  
  94.     Sint16 InputEngine::getJoyXAxis()
  95.     {
  96.         Sint16 val = 0;
  97.  
  98.         if(m_pJoystick)
  99.         {
  100.             val = SDL_JoystickGetAxis(m_pJoystick, 0) / JOYSTICK_DEAD_ZONE;
  101.         }
  102.  
  103.         return( val );
  104.     }
  105.  
  106.     Sint16 InputEngine::getJoyYAxis()
  107.     {
  108.         Sint16 val = 0;
  109.  
  110.         if(m_pJoystick)
  111.         {
  112.             val = SDL_JoystickGetAxis(m_pJoystick, 1) / JOYSTICK_DEAD_ZONE;
  113.         }
  114.  
  115.         return( val );
  116.     }
  117. }